package com.zz.zzliving.commodity.controller;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.zz.zzliving.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author 张哲
 * @email a252013135@163.com
 * @date 2024/4/14 13:52
 */
@RestController
@RequestMapping("/oss")
@Slf4j
public class OSSController {

    @Resource
    private OSSClient ossClient;

    @RequestMapping("/cloudUP")
    public R testUpload2() throws FileNotFoundException {
        log.info("cloud Upload");
        InputStream inputStream =
                new FileInputStream("C:\\Users\\86156\\Desktop\\zhangoss.png");
//        String fileName = "images/".concat(UUID.randomUUID().toString().concat(".png"));
//        System.out.println("fileName = " + fileName);
        String imgName = UUID.randomUUID().toString();
        String fileName = "images/" + imgName + ".png";
        ossClient.putObject("zzliving-01", fileName, inputStream);
        ossClient.shutdown();
        System.out.println("上传 ok~");
        return R.ok("上传 ok~");
    }


    @GetMapping("/upFile")
    public R upFile(@RequestParam("filePath") String filePath) throws Exception {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "zzliving-01";
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String imgName = UUID.randomUUID().toString();
        String objectName = "images/" + imgName + ".png";
        filePath = "C:\\Users\\86156\\Desktop\\".concat(filePath);
//        String filePath = "C:\\Users\\86156\\Desktop\\zhangoss.png";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            InputStream inputStream = new FileInputStream(filePath);
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return R.ok();
    }

    @GetMapping("/test")
    public String test(String test) {
        System.out.println("test = " + test);
        return "success";
    }
}
